package com.supermap.dialogs;

import com.supermap.data.*;
import com.supermap.dialogs.advance.CJDialog;
import com.supermap.mapping.ThemeUnique;
import com.supermap.mapping.ThemeUniqueItem;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

public class ThemeUniqueDialog extends CJDialog {
    private JComboBox<String> valueComboBox;

    private DatasetVector datasetVector;
    private ThemeUnique themeUnique;
    private int startColorRGB;
    private int endColorRGB;
    private ArrayList<ThemeUniqueItem> themeUniqueItems;

    public ThemeUniqueDialog(Frame owner, String title, boolean modal, DatasetVector datasetVector) {
        super(owner, title, modal, 260, 140, new Object[]{datasetVector});
    }

    public ThemeUnique getThemeUnique() {
        return themeUnique;
    }

    @Override
    protected void initializeArgs(Object[] args) {
        this.datasetVector = (DatasetVector) args[0];
        startColorRGB = Color.WHITE.getRGB();
        endColorRGB = Color.BLACK.getRGB();
    }

    @Override
    protected void initializeCore() {
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                super.windowClosed(e);
                themeUnique = null;
            }
        });
        super.confirmBtn.setText("创建");

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(3, 1));
        add(mainPanel, BorderLayout.CENTER);

        // 下拉框
        valueComboBox = new JComboBox<>();
        FieldInfos fieldInfos = datasetVector.getFieldInfos();
        for (int i = 0; i < fieldInfos.getCount(); i++) {
            FieldInfo fieldInfo = fieldInfos.get(i);
            FieldType fieldType = fieldInfo.getType();
            if (fieldType != FieldType.LONGBINARY && fieldType != FieldType.JSONB) {
                valueComboBox.addItem(fieldInfo.getCaption());
            }
        }

        themeUniqueItems = new ArrayList<>();
        valueComboBox.addItemListener(e -> {
            String fieldName = (String) e.getItem();
            fillThemeUniqueItems(datasetVector, fieldName, themeUniqueItems);
        });
        valueComboBox.setSelectedIndex(0);
        fillThemeUniqueItems(datasetVector, (String) valueComboBox.getSelectedItem(), themeUniqueItems);
        mainPanel.add(valueComboBox);

        // 边界颜色设置
        JButton startColorButton = new JButton();
        startColorButton.setText("起始颜色");
        startColorButton.setBackground(Color.WHITE);
        startColorButton.addActionListener(e -> {
            Color startColor = JColorChooser.showDialog(this, "起始颜色", Color.WHITE);
            startColorButton.setBackground(startColor);
            startColorRGB = startColor.getRGB();
            fillThemeUniqueItems(datasetVector, (String) valueComboBox.getSelectedItem(), themeUniqueItems);
        });
        mainPanel.add(startColorButton);

        JButton endColorButton = new JButton();
        endColorButton.setText("结束颜色");
        endColorButton.setBackground(Color.BLACK);
        endColorButton.addActionListener(e -> {
            Color endColor = JColorChooser.showDialog(this, "结束颜色", Color.BLACK);
            endColorButton.setBackground(endColor);
            endColorRGB = endColor.getRGB();
            fillThemeUniqueItems(datasetVector, (String) valueComboBox.getSelectedItem(), themeUniqueItems);
        });
        mainPanel.add(endColorButton);
    }

    @Override
    protected void doConfirmCore() {
        setVisible(false);
        themeUnique = new ThemeUnique();
        themeUnique.setUniqueExpression((String) valueComboBox.getSelectedItem());
        GeoStyle defaultGeoStyle = new GeoStyle();
        defaultGeoStyle.setFillBackColor(Color.GREEN);
        defaultGeoStyle.setFillForeColor(Color.YELLOW);
        defaultGeoStyle.setFillGradientMode(FillGradientMode.RADIAL);
        themeUnique.setDefaultStyle(defaultGeoStyle);
        for (ThemeUniqueItem themeUniqueItem : themeUniqueItems) {
            themeUnique.add(themeUniqueItem);
        }
    }

    private void fillThemeUniqueItems(DatasetVector datasetVector, String fieldName, ArrayList<ThemeUniqueItem> themeUniqueItems) {
        if (themeUniqueItems == null) {
            return;
        }

        themeUniqueItems.clear();
        Recordset recordset = datasetVector.getRecordset(false, CursorType.DYNAMIC);
        int recordCount = recordset.getRecordCount();
        int colorStep = (endColorRGB - startColorRGB) / recordCount;
        int tempColorRGB = startColorRGB - colorStep;
        do {
            Object fieldValue = recordset.getFieldValue(fieldName);
            if (fieldValue != null) {
                String value = fieldValue.toString();
                ThemeUniqueItem themeUniqueItem = new ThemeUniqueItem();
                themeUniqueItem.setUnique(value);
                themeUniqueItem.setCaption(fieldName + " = '" + value + "'");
                GeoStyle style = new GeoStyle();
                style.setFillBackColor(Color.BLACK);
                style.setFillForeColor(new Color(tempColorRGB += colorStep));
                themeUniqueItem.setStyle(style);
                themeUniqueItems.add(themeUniqueItem);
            }
        } while (recordset.moveNext());
        recordset.close();
        recordset.dispose();
    }
}
